# 绘制字符串，参数依次为：x, y, 字符高度，字符串，字符颜色（RGB三元组）
img.draw_string_advanced(50, 50, 80, "hello k230\n学不会电磁场", color=(255, 0, 0))

# 绘制直线，参数依次为：x1, y1, x2, y2, 颜色，线宽
img.draw_line(50, 50, 300, 130, color=(0, 255, 0), thickness=2)

# 绘制方框，参数依次为：x, y, w, h, 颜色，线宽，是否填充
img.draw_rectangle(1000, 50, 300, 200, color=(0, 0, 255), thickness=4, fill=False)

# 绘制关键点, 列表[(x, y, 旋转角度)]
img.draw_keypoints([[960, 540, 200]], color=(255, 255, 0), size=30, thickness=2, fill=False)

# 绘制圆, x, y, r
img.draw_circle(640, 640, 50, color=(255, 0, 255), thickness=2, fill=True)

#矩形识别，可以用来找矩形的四个角的坐标
img_rect = img.to_grayscale(copy=True) # 复制一张图，将其转换为灰度格式，用于提升帧率（转换为其他格式也同理）

img_rect = img_rect.binary([(82, 212)]) # 图像进行二值化

rects = img_rect.find_rects(threshold=10000) # 识别矩形

if not rects == None:
    for rect in rects:
        corner = rect.corners()
        img.draw_line(corner[0][0], corner[0][1], corner[1][0], corner[1][1], color=(0, 255, 0), thickness=5)
        img.draw_line(corner[2][0], corner[2][1], corner[1][0], corner[1][1], color=(0, 255, 0), thickness=5)
        img.draw_line(corner[2][0], corner[2][1], corner[3][0], corner[3][1], color=(0, 255, 0), thickness=5)
        img.draw_line(corner[0][0], corner[0][1], corner[3][0], corner[3][1], color=(0, 255, 0), thickness=5)

img.draw_string_advanced(50, 50, 80, "fps: {}".format(clock.fps()), color=(255, 0, 0)) # 打印帧率

img.compressed_for_ide() # 压缩图像到合适大小，防止扭曲

img_line.midpoint_pool(2, 2) # 将图像的横纵大小都缩小为原来的1/2
lines = img_line.find_line_segments((0, 0, 1024//2, 768//2), 15, 15) # 缩小大小
line.x1()*2, line.y1()*2, line.x2()*2, line.y2()*2 # 坐标要乘以二保持不变

fpioa = FPIOA()
fpioa.help() # 终端显示引脚作用表
fpioa.set_function(33, FPIOA.GPIO33)
pin = Pin(33, Pin.OUT)
pin.value(1) # 亮灯

img = img.copy(roi=()) # 图像裁切，和图像压缩一起使用

def send_three_uchar(c1, c2, c3):
    global uart
    header = 0xAA
    total_bits = 6
    data_parts = [header, total_bits, c1, c2, c3]
    data = ustruct.pack("<BBBBB", *data_parts)
    checksum = sum(data_parts)
    data += ustruct.pack("<B", checksum)
    if uart is not None:
        uart.write(data)
        hex_data = binascii.hexlify(data).decode('utf-8')
        print(f"发送16进制数据:{hex_data}")
    return data  # 串口发送




